﻿$(function () {
    var url = new Url();
    var $Id = url.GetQueryString("id");
    var $css = url.GetQueryString("css");
    if (!$Id) {//保存模式
        $("#IsCreateDiv").css("display", "none");
    }
    else {//编辑模式，获取并绑定短信模板
        $.ajax({
            url: "../Handler/BankInfoHandler.ashx",
            type: "get",
            data: { "type": 4, "Id": $Id, _: Math.random() },
            dataType: 'json',
            cache: false,
            success: function (result) {
                if (result.Status) {
                    $("#txtBankName").val(result.Data.BankName);
                    $("#txtBankInfoId").val(result.Data.BankInfoId);
                    $("#txtBankCode").val(result.Data.BankCode);
                    $("#txtLimitAmount").val(result.Data.LimitAmount);
                    $("#txtLimitNumber").val(result.Data.LimitNumber);
                    $("#txtIntervalTime").val(result.Data.IntervalTime);
                    if (result.Data.IsUploadCredentials) {
                        $("#radioIsUploadCredentialsYes").attr("checked", "checked");
                    } else {
                        $("#radioIsUploadCredentialsNo").attr("checked", "checked");
                    }
                    if (result.Data.CanGetOrder) {
                        $("#radioCanGetOrderYes").attr("checked", "checked");
                    } else {
                        $("#radioCanGetOrderNo").attr("checked", "checked");
                    }
                    if (result.Data.IsShow) {
                        $("#radioIsShowYes").attr("checked", "checked");
                    } else {
                        $("#radioIsShowNo").attr("checked", "checked");
                    }
                    if (result.Data.IsActive) {
                        $("#radioIsActiveYes").attr("checked", "checked");
                    } else {
                        $("#radioIsActiveNo").attr("checked", "checked");
                    }
                    $("#txtCreateOptUser").val(result.Data.CreateOptUser);
                    $("#txtLastOptUser").val(result.Data.LastOptUser);
                    $("#txtCreateDate").val(data_string(result.Data.CreateDate));
                    $("#txtLastUptDate").val(data_string(result.Data.LastUptDate));
                } else {
                    alert(result.Message);
                }
            }
        });
    }
    //保存
    $(".btnSave").bind("click", function () {
        var $BankInfoId = $("#txtBankInfoId").val();
        var $BankName = $("#txtBankName").val();
        var $BankCode = $("#txtBankCode").val();
        var $LimitAmount = $("#txtLimitAmount").val();
        var $LimitNumber = $("#txtLimitNumber").val();
        var $IntervalTime = $("#txtIntervalTime").val();
        var $IsUploadCredentials = $('input:radio[name="IsUploadCredentials"]:checked').val();
        var $CanGetOrder = $('input:radio[name="CanGetOrder"]:checked').val();
        var $IsShow = $('input:radio[name="IsShow"]:checked').val();
        var $IsActive = $('input:radio[name="IsActive"]:checked').val();

        var errorNode = null;
        $("#BankInfo .text").each(function () {
            $(this).next("span").text("");
            if (!$(this).val() || $(this).val() == -1) {
                errorNode = $(this);
                $name = $(this).attr("data-name");
                $(this).next("span").text($name + "不能为空！");
                $(this).focus();
                return;
            }
        });
        if (errorNode) {
            return;
        }
        var validate = new Validate();
        $(".errorBankCode").text("");
        if (!validate.CheckNum($BankCode)) {
            if (!$BankCode == "") {
                $(".txtBankCode").focus();
                $(".errorBankCode").text("不是数字格式");
                return;
            }
        }
        $(".errorLimitAmount").text("");
        if (!validate.CheckNum($LimitAmount)) {
            if (!$LimitAmount == "") {
                $(".txtLimitAmount").focus();
                $(".errorLimitAmount").text("不是数字格式");
                return;
            }
        }
        $(".errorLimitNumber").text("");
        if (!validate.CheckNum($LimitNumber)) {
            if (!$LimitNumber == "") {
                $(".txtLimitNumber").focus();
                $(".errorLimitNumber").text("不是数字格式");
                return;
            }
        }
        $(".errorIntervalTime").text("");
        if (!validate.CheckNum($IntervalTime)) {
            if (!$IntervalTime == "") {
                $(".txtIntervalTime").focus();
                $(".errorIntervalTime").text("不是数字格式");
                return;
            }
        }

        var type = $Id ? 2 : 1;
        $.ajax({
            url: "../Handler/BankInfoHandler.ashx",
            type: "post",
            data: {
                type: type,
                BankInfoId: $BankInfoId,
                BankName: $BankName,
                BankCode: $BankCode,
                LimitAmount: $LimitAmount,
                LimitNumber: $LimitNumber,
                IntervalTime: $IntervalTime,

                IsUploadCredentials: $IsUploadCredentials,
                CanGetOrder: $CanGetOrder,
                IsShow: $IsShow,
                IsActive: $IsActive,
                _: Math.random()
            },
            dataType: 'json',
            cache: false,
            success: function (result) {
                if (result.Status) {
                    location.href = "BankInfoList.aspx?css=" + $css;
                }
                else {
                    alert(result.Message);
                }
            }
        });
    });
});

//时间字符串转换成时间格式
function data_string(str) {
    var d = eval('new ' + str.substr(1, str.length - 2));
    var ar_date = [d.getFullYear(), d.getMonth() + 1, d.getDate()];
    for (var i = 0; i < ar_date.length; i++) ar_date[i] = dFormat(ar_date[i]);
    return ar_date.join('-');
    function dFormat(i) { return i < 10 ? "0" + i.toString() : i; }
}